﻿using DbControl;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CentralMonitoring
{
    /// <summary>
    /// 设备终端配置窗体
    /// </summary>
    public partial class SensorConfigEdit : Form
    {    
        public int m_Id = -1;
        public int m_ParentId = 0;      
        public string m_beforName = "";
        private string m_beforCode = "";
        private devices m_devices;
        public SensorConfigEdit()
        {
            InitializeComponent();
        }
        private void InitCombox()
        {
            DataTable dt = OprDeviceConfig.GetCongigSensors();
            if (dt != null)
            {
                DataRow row = dt.NewRow();
                row["id"] = -1;
                row["sensor_code"] = "请选择";
                row["sensor_name"] = "请选择";
                dt.Rows.InsertAt(row,0);
                comboBoxSensor.DataSource = dt;
                comboBoxSensor.DisplayMember = "sensor_name";
                comboBoxSensor.ValueMember = "id";
                if (m_devices != null && m_devices.sensor_id.HasValue)
                {
                    sensors sensor = OprSensor.GetSensorById(m_devices.sensor_id.Value);
                    comboBoxSensor.Text = sensor.sensor_name;
                }
                else
                {
                    comboBoxSensor.SelectedIndex = 0;
                }
            }
        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            int sensorId =(int)comboBoxSensor.SelectedValue;
            if (sensorId > 0)
            {
                if (OprDeviceConfig.IsExistSensorId(sensorId))
                {
                    MessageBox.Show("此终端已关联过设备，请确认后重试！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (OprDeviceConfig.SetSensor(m_Id, sensorId))
                {

                    MessageBox.Show("终端设置成功！", "提示", MessageBoxButtons.OK);
                    DialogResult = DialogResult.OK;
                }
            }
            else
            {
                if (OprDeviceConfig.ClearSensor(m_Id))
                {

                    MessageBox.Show("终端解除关联！", "提示", MessageBoxButtons.OK);
                    DialogResult = DialogResult.OK;
                }
            }
           
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DeviceConfigEdit_Load(object sender, EventArgs e)
        {
            if (m_Id > 0)
            {
                m_devices = OprDevice.GetdevicesById(m_Id);
                if (m_devices != null)
                {
                    
                    textBoxCode.Text = m_devices.device_code;
                    textBoxName.Text = m_devices.device_name;                   
                    textBoxSite.Text = m_devices.device_position;    
                }
            }
            InitCombox();
        }
    }
}
